home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- *
- * EdWindows.c Defines types and code for generic text editor windows
- *
- * Modification History:
- * 18/2/92 created from scratch.
- *
- *
- *
- *************************************************************************************/
- #include "EditGlobalEqu.p"
-
- #include "EdWindows.proto.h"
-
-
- textEdHdl NewEditRecord(WindowPtr ownerWindow);
-
-
-
- char IsEditKind(WindowPtr theWindow)
- {
- /* returns TRUE if window is editor kind */
-
- if (theWindow != NIL)
- return((*(WindowPeek)theWindow).windowKind == EditWindowKind);
- else
- return(FALSE);
- }
-
-
- textEdHdl GetWEditRecord(WindowPtr theWindow)
- {
- /* returns handle to window's edit record, if window is editkind */
- if (IsEditKind(theWindow))
- return ((textEdHdl)GetWRefCon(theWindow));
- }
-
-
- DrawEditWindow(WindowPtr theWindow)
- {
- /* call for edit type window to draw the window called from update event so
- is current port and visRgn set up */
-
- DrawTabBar(theWindow,NIL);
- SetWindowClip(theWindow);
- DrawList(theWindow,NIL);
- UnClipWindow(theWindow);
- DrawControls(theWindow);
- DrawGrowIcon(theWindow);
- }
-
-
- DrawTabBar(WindowPtr theWindow,RgnHandle updateRgn)
- {
- /* draws tab bar for the edit window- at present just draws delimiting line */
-
- Rect tr,mr;
- CTabHandle tbColours;
- OSErr theErr;
- SysEnvRec macEnv;
- int index,saveFont,saveSize,chCnt;
- int hOrigin,charPix,tH,isColour;
- RgnHandle saveClip;
- Str32 rulerText;
- textEdHdl theText;
- GDHandle theDevice;
-
- saveClip = NewRgn();
- GetClip(saveClip);
- theText = GetWEditRecord(theWindow);
- if (theText != NIL) {
- hOrigin = GetMarginOffset(theText);
- charPix = (*theText)->mSpace;
- tH = GetTabBarHeight(theText);
- }
-
- if (tH != 0) {
-
- SetRect(&tr,thePort->portRect.left,thePort->portRect.top
- ,thePort->portRect.right-15,thePort->portRect.top + tH-2);
-
- if (updateRgn != NIL)
- SetClip(updateRgn);
- else
- ClipRect(&tr);
-
- isColour = FALSE;
- theErr = SysEnvirons(2,&macEnv);
- if (theErr == noErr && macEnv.hasColorQD) {
- mr = tr;
- LocalToGlobal(&topLeft(mr));
- LocalToGlobal(&botRight(mr));
- theDevice = GetMaxDevice(&mr);
- if (theDevice != NIL )
- isColour = (**(*theDevice)->gdPMap).pixelSize > 2;
-
- }
-
- if (isColour) {
- tbColours = GetCTable(standardResID);
- if (tbColours != NIL) {
- RGBForeColor(&(*tbColours)->ctTable[1].rgb);
- PaintRect(&tr);
- ForeColor(blackColor);
- DisposeCTable(tbColours);
- }
- }
-
- saveFont = thePort->txFont;
- saveSize = thePort->txSize;
- TextFont(geneva);
- TextSize(9);
- MoveTo(thePort->portRect.left + hOrigin,thePort->portRect.top+10);
-
- for (index =0;index<(charPix * 256);index += charPix) {
- if ((index % 20) == 0) {
- NumToString(index/charPix,&rulerText);
- Move(-(StringWidth(&rulerText)/2),-1);
- DrawString(&rulerText);
- Move(-(StringWidth(&rulerText)/2),1);
- PenSize(2,1);
- }
- Line(0,5);
- PenNormal();
- Line(charPix,0);
- Move(0,-5);
- }
- /*DrawTabMarkers(theText,hOrigin);*/
- SetClip(saveClip);
- TextFont(saveFont);
- TextSize(saveSize);
- MoveTo(thePort->portRect.left,thePort->portRect.top + tH -2);
- LineTo(thePort->portRect.right-15,thePort->portRect.top+ tH -2);
- MoveTo(thePort->portRect.left,thePort->portRect.top+ tH);
- LineTo(thePort->portRect.right-15,thePort->portRect.top+ tH);
- }
- DisposeRgn(saveClip);
- }
-
-
- DrawTabMarkers(textEdHdl theText,int margin)
- {
- /* draws tab markers. */
-
- int i,tabStop;
-
- MoveTo(thePort->portRect.left + margin,thePort->portRect.top+20);
- if (theText != NIL) {
- for(i=0;i<10;i++){
- tabStop = (*theText)->tabs[i];
- Move(tabStop * (*theText)->mSpace,0);
- Line(0,6);
- Move(-tabStop * (*theText)->mSpace,-6);
- }
- }
- }
-
- InitEditWindow(WindowPtr theWindow)
- {
- /* initialises editor window, when window created. Sets up scrollbars etc */
-
- Rect r,wPort;
- ControlHandle sBar;
- textEdHdl wEditRec;
-
- if (theWindow != NIL) {
- wEditRec = NewEditRecord(theWindow);
- wPort = theWindow->portRect;
- r = wPort;
- r.left = r.right-15;
- r.bottom -=15;
- r.right ++;
- sBar = NewControl(theWindow,&r,NIL,TRUE,1,1,1,scrollBarProc,0L);
- if (wEditRec != NIL)
- (*wEditRec)->vScroll = sBar;
- r = wPort;
- r.top = r.bottom -15;
- r.right -=15;
- r.bottom++;
- sBar = NewControl(theWindow,&r,NIL,TRUE,1,1,256,scrollBarProc,0L);
- if (wEditRec != NIL)
- (*wEditRec)->hScroll = sBar;
- (*(WindowPeek)theWindow).windowKind = EditWindowKind;
- InitEdDisplay(theWindow);
- }
- }
-
-
- DisposeEditWindow(WindowPtr theWindow)
- {
- /* removes all edit data structures, including linked list, edit record, window rec */
-
- textEdHdl theTextRec;
-
- if (IsEditKind(theWindow)) {
- theTextRec = GetWEditRecord(theWindow);
- DisposeEdRecord(theTextRec);
- }
- DisposeWindow(theWindow);
- }
-
-
- GetScrollBars(WindowPtr theWindow,ControlHandle *v,ControlHandle *h)
- {
- /* returns scroll bar handles from edit record */
-
- textEdHdl theEd;
-
- if (IsEditKind(theWindow)) {
- theEd = GetWEditRecord(theWindow);
- if (theEd != NIL) {
- *v = (*theEd)->vScroll;
- *h = (*theEd)->hScroll;
- }
- }
- }
-
-
- ReCalcLineRange(WindowPtr theWindow)
- {
- /* call whenever window is resized to recalculate the number of visible lines in the
- window, and adjust scroll bar ranges accordingly */
-
- textEdHdl theEd;
- ControlHandle vS,hS;
- int scMax;
-
- if (IsEditKind(theWindow)) {
- theEd = GetWEditRecord(theWindow);
- if (theEd != NIL) {
- GetScrollBars(theWindow,&vS,&hS);
- (*theEd)->nLines = GetNumberLines(theEd);
- (*theEd)->linesInWindow = GetLinesFit(theWindow);
- (*theEd)->charsAcross = GetHCharsFit(theWindow,(*theEd)->mSpace);
- scMax = (*theEd)->nLines;
- if (scMax < GetCtlMin(vS))
- scMax = GetCtlMin(vS);
- SetCtlMax(vS,scMax);
- (*theEd)->topLine = GetCtlValue(vS);
-
- SetCtlMax(hS,256 - (*theEd)->charsAcross);
- }
- }
- }
-
-
- MoveScrollbars(WindowPtr theWindow,ControlHandle vS,ControlHandle hS)
- {
- /* code stub shared by ResizeWindow and ZoomScrollWindow to move the scrollbars to
- the window edges. The window should have been set to the new size & bars hidden
- before calling, and theWindow must be current port */
-
- Rect tempRect;
-
- if (theWindow!=NIL && vS!=NIL && hS!=NIL){
- tempRect=(**vS).contrlRect;
- tempRect.bottom+=15; /* erase the grow icon */
- EraseRect(&tempRect);
- InvalRect(&tempRect);
- tempRect=(**hS).contrlRect;
- EraseRect(&tempRect);
- InvalRect(&tempRect);
- tempRect=theWindow->portRect;
-
- /* move the bars to the window edges, resizing if necessary */
-
- MoveControl(vS,tempRect.right-15,tempRect.top-1);
- SizeControl(vS,16,tempRect.bottom-tempRect.top-13);
-
- MoveControl(hS,tempRect.left-1,tempRect.bottom-15);
- SizeControl(hS,tempRect.right-tempRect.left-13,16);
-
- tempRect=(**vS).contrlRect;
- ValidRect(&tempRect);
- tempRect=(**hS).contrlRect;
- ValidRect(&tempRect);
- tempRect.right=tempRect.left;
- tempRect.left=0;
- EraseRect(&tempRect);
- InvalRect(&tempRect);
- ShowControl(vS);
- ShowControl(hS);
- }
- }
-
-
- ResizeEdWindow(WindowPtr theWindow,int width,int height)
- {
- /* call in response to a grow or zoom request for the window- This routine moves the
- scrollbars referenced by vS (vertical) and hS (horizontal), sorts out the various
- update region manipulations required, and sets the clip region for the window to
- exclude the scrollbars. It draws the controls as required, but removes them from
- the update region to stop them being drawn twice. */
-
- Rect tempRect;
- GrafPtr savePort;
- ControlHandle vS,hS;
- int vCheck;
-
- if (theWindow!=NIL) {
- GetPort(&savePort);
- SetPort(theWindow);
-
- if (IsEditKind(theWindow))
- GetScrollBars(theWindow,&vS,&hS);
-
- vCheck = GetCtlValue(vS);
-
- /* first, hide the bars and mark their areas for update */
-
- if (vS!=NIL)
- HideControl(vS);
- if (hS!=NIL)
- HideControl(hS);
-
- SizeWindow(theWindow,width,height,TRUE);
-
- MoveScrollbars(theWindow,vS,hS);
- ReCalcLineRange(theWindow);
- DrawGrowIcon(theWindow);
- if (vCheck != GetCtlValue(vS)) {
- /* resizing altered the scroll position, so redraw the lot */
- GetClipRect(theWindow,&tempRect);
- EraseRect(&tempRect);
- InvalRect(&tempRect);
- }
- SetPort(savePort);
- }
- }
-
-
- ZoomEdScrollWindow(WindowPtr theWindow,int partCode,short front)
- {
- /* replaces ZoomWindow function when window has scrollbars */
-
- ControlHandle vS,hS;
-
- if (theWindow!=NIL && IsEditKind(theWindow)) {
- GetScrollBars(theWindow,&vS,&hS);
- if (vS!=NIL && hS!=NIL) {
- HideControl(vS);
- HideControl(hS);
- }
- ZoomWindow(theWindow,partCode,front);
- MoveScrollbars(theWindow,vS,hS);
- ReCalcLineRange(theWindow);
- DrawGrowIcon(theWindow);
- }
- }
-
-
- DisposeAllWindows(WindowPtr wListHead)
- {
- /* calls disposeEdWindow for all windows in window list */
- WindowPtr w;
-
- while (wListHead != NIL) {
- w = (*(WindowPeek)wListHead).nextWindow;
- DisposeEditWindow(wListHead);
- wListHead = w;
- }
- }
-
-
- pascal void ScrollProc(ControlHandle theControl,int partCode)
- {
- /* action procedure for scrolling text in edit windows. To get text info, etc, the
- text record is got from the control's owner window, so this applies ONLY to ed
- windows */
-
- textEdHdl theText;
- ControlHandle vS,hS;
- int lHeight;
- int amount,pageAmt;
- WindowPtr edWindow;
- RgnHandle upRgn,tempRgn;
- Rect sr;
-
- edWindow = (*theControl)->contrlOwner;
- if (edWindow != NIL) {
- theText = GetWEditRecord(edWindow);
- GetScrollBars(edWindow,&vS,&hS);
- if (theControl==vS) {
- lHeight = GetLineHeight(edWindow); /* vertical spacing */
- pageAmt = (*theText)->linesInWindow -1;
- }
- else {
- lHeight = (*theText)->mSpace; /* horizontal spacing */
- pageAmt = (*theText)->charsAcross/2;
- }
- }
- amount = GetCtlValue(theControl);
- upRgn = NewRgn();
-
- switch (partCode) {
- case inUpButton:
- SetCtlValue(theControl,GetCtlValue(theControl)-1);
- break;
- case inDownButton:
- SetCtlValue(theControl,GetCtlValue(theControl)+1);
- break;
- case inPageUp:
- SetCtlValue(theControl,GetCtlValue(theControl)-pageAmt);
- break;
- case inPageDown:
- SetCtlValue(theControl,GetCtlValue(theControl)+pageAmt);
- break;
- }
- GetClipRect(edWindow,&sr);
- if (theControl == vS) {
- amount = (amount - GetCtlValue(theControl)) * lHeight;
- if (amount != 0) {
- ClipRect(&sr);
- (*theText)->topLine = GetCtlValue(theControl);
- ScrollRect(&sr,0,amount,upRgn);
- DrawList(edWindow,upRgn);
- }
- }
- else
- if (theControl == hS) {
- amount = (amount - GetCtlValue(theControl)) * lHeight;
- if (amount != 0) {
- tempRgn = NewRgn();
- RectRgn(tempRgn,&sr);
- sr.top = thePort->portRect.top;
- ClipRect(&sr);
- ScrollRect(&sr,amount,0,upRgn);
- SectRgn(upRgn,tempRgn,tempRgn);
- DrawList(edWindow,tempRgn);
- sr.bottom = sr.top + GetTabBarHeight(theText) +2;
- RectRgn(tempRgn,&sr);
- SectRgn(upRgn,tempRgn,tempRgn);
- DrawTabBar(edWindow,tempRgn);
- DisposeRgn(tempRgn);
- }
- }
- UnClipWindow(edWindow);
- DisposeRgn(upRgn);
- }
-
-
- ClickEditWindow(WindowPtr theWindow,Point clickPt)
- {
- /* handle clicks for edit window. Window is valid edit window, current port and click
- is in local coords. All handed to you on a plate! */
-
- textEdHdl theText;
- ControlHandle vS,hS,theControl;
- int partCode;
- Rect tr;
-
- theText = GetWEditRecord(theWindow);
- if (theText != NIL) {
- GetScrollBars(theWindow,&vS,&hS);
-
- partCode = FindControl(clickPt,theWindow,&theControl);
-
- if (partCode == inThumb) {
- partCode = TrackControl(theControl,clickPt,NIL);
- if (partCode != 0) {
- ReCalcLineRange(theWindow);
- GetClipRect(theWindow,&tr);
- if (theControl == hS)
- tr.top-= GetTabBarHeight(theText)+2;
- EraseRect(&tr);
- InvalRect(&tr);
- }
- }
- else {
- switch (partCode) {
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- partCode = TrackControl(theControl,clickPt,&ScrollProc);
- ReCalcLineRange(theWindow);
- break;
- default:
- ClickText(theWindow,clickPt);
- break;
- }
- }
- }
- }
-
-
- int ShowHideRulers(WindowPtr theWindow)
- {
- /* toggles existence of ruler bar in window */
-
- textEdHdl theText;
- GrafPtr savePort;
- Rect pr;
- int whichAction;
-
- if (IsEditKind(theWindow)) {
- theText = GetWEditRecord(theWindow);
- if (GetTabBarHeight(theText) == 0) {
- SetTabBarHeight(theText,TabBarHeight);
- whichAction = TRUE;
- }
- else {
- SetTabBarHeight(theText,0);
- whichAction = FALSE;
- }
-
- GetPort(&savePort);
- SetPort(theWindow);
- pr = thePort->portRect;
- pr.right -=15;
- pr.bottom -= 15;
- EraseRect(&pr);
- InvalRect(&pr);
- SetPort(savePort);
- ReCalcLineRange(theWindow);
-
- return(whichAction);
- }
- }
-
-
- OpenSourceCode(void)
- {
- /* high level menu command to open a source file and display it in an edit window */
-
- WindowPtr w;
- OSErr theErr;
- SysEnvRec macEnv;
- SFReply theReply;
-
- ChooseTextFile(&theReply);
- if (theReply.good) {
- theErr = SysEnvirons(2,&macEnv);
- if (theErr == noErr && macEnv.hasColorQD)
- w = GetNewCWindow(standardResID+1,NIL,(WindowPtr)-1L);
- else
- w = GetNewWindow(standardResID+1,NIL,(WindowPtr)-1L);
-
- if (w != NIL) {
- ShowWindow(w);
- InitEditWindow(w);
- SelectWindow(w);
- LoadText(&theReply,w);
- }
- }
- }
-